package org.javaforever.cookieshop.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.javaforever.cookieshop.domain.CustOrderBean;
import org.javaforever.cookieshop.domain.OrderItemBean;
import org.javaforever.cookieshop.domain.UserBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.UserService;
import org.javaforever.cookieshop.serviceimpl.CustOrderServiceImpl;
import org.javaforever.cookieshop.utils.BooleanUtil;
import org.javaforever.cookieshop.utils.PDFUtil;
import org.javaforever.cookieshop.utils.POIExcelUtil;
import org.javaforever.cookieshop.utils.POIPPTUtil;
import org.javaforever.cookieshop.utils.StringUtil;
import org.javaforever.cookieshop.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/custOrderController")
@RestController
public class CustOrderController {
	public CustOrderBean tempCustOrder = new CustOrderBean();
	@Autowired
	protected UserService userService;
	@Autowired
	protected CustOrderService service;

	@RequestMapping(value = "/activateAllCustOrders", method = RequestMethod.POST)
	public Map<String,Object> activateAllCustOrders(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateAllCustOrders(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/activateCustOrder", method = RequestMethod.POST)
	public Map<String,Object> activateCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addCustOrder", method = RequestMethod.POST)
	public Map<String,Object> addCustOrder(@RequestBody CustOrderBean custOrder) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.addCustOrder(custOrder);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/assignOrderItemsToCustOrder", method = RequestMethod.POST)
	public Map<String,Object> assignOrderItemsToCustOrder(Long custOrderId,String orderItemIds) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        boolean success = service.assignOrderItemsToCustOrder(custOrderId,orderItemIds);
        result.put("success",success);
        result.put("rows",null);
        return result;
	}
	@RequestMapping(value = "/checkAccessCustOrder", method = RequestMethod.GET)
	public Map<String,Object> checkAccessCustOrder() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneAllCustOrders", method = RequestMethod.POST)
	public Map<String,Object> cloneAllCustOrders(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneAllCustOrders(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneCustOrder", method = RequestMethod.POST)
	public Map<String,Object> cloneCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteAllCustOrders", method = RequestMethod.POST)
	public Map<String,Object> deleteAllCustOrders(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteAllCustOrders(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteCustOrder", method = RequestMethod.POST)
	public Map<String,Object> deleteCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/exportCustOrders")
	public void exportCustOrders(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.xls");
            List<CustOrderBean> list = service.listAllCustOrders();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user != null) {
                    userType0 = userService.findUserById(user);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportCustOrdersPDF")
	public void exportCustOrdersPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.pdf");
            List<CustOrderBean> list = service.listAllCustOrders();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user != null) {
                    userType0 = userService.findUserById(user);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportCustOrdersWord")
	public void exportCustOrdersWord(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.doc");
            List<CustOrderBean> list = service.listAllCustOrders();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user != null) {
                    userType0 = userService.findUserById(user);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportPPTCustOrders")
	public void exportPPTCustOrders(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.pptx");
            List<CustOrderBean> list = service.listAllCustOrders();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user != null) {
                    userType0 = userService.findUserById(user);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterCustOrdersExcel", method = RequestMethod.GET)
	public void filterCustOrdersExcel(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "total", required = false) Double total,
		@RequestParam(value = "amount", required = false) Integer amount,
		@RequestParam(value = "status", required = false) Integer status,
		@RequestParam(value = "paytype", required = false) Integer paytype,
		@RequestParam(value = "phone", required = false) String phone,
		@RequestParam(value = "address", required = false) String address,
		@RequestParam(value = "orderDateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date orderDateTime,
		@RequestParam(value = "user", required = false) Long user) throws Exception{
        CustOrderBean custOrder0 = new CustOrderBean();
        if (!StringUtil.isBlank(name)) custOrder0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) custOrder0.setActive(activeBool);
        if (total != null) custOrder0.setTotal(total);
        if (amount != null) custOrder0.setAmount(amount);
        if (status != null) custOrder0.setStatus(status);
        if (paytype != null) custOrder0.setPaytype(paytype);
        if (!StringUtil.isBlank(phone)) custOrder0.setPhone(phone);
        if (!StringUtil.isBlank(address)) custOrder0.setAddress(address);
        if (orderDateTime != null) {
            custOrder0.setOrderDateTime(orderDateTime);
        }
        if (user != null) custOrder0.setUser(user);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.xls");
            List<CustOrderBean> list = service.searchCustOrdersByFields(custOrder0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user0 = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user0 != null) {
                    userType0 = userService.findUserById(user0);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterCustOrdersPDF", method = RequestMethod.GET)
	public void filterCustOrdersPDF(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "total", required = false) Double total,
		@RequestParam(value = "amount", required = false) Integer amount,
		@RequestParam(value = "status", required = false) Integer status,
		@RequestParam(value = "paytype", required = false) Integer paytype,
		@RequestParam(value = "phone", required = false) String phone,
		@RequestParam(value = "address", required = false) String address,
		@RequestParam(value = "orderDateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date orderDateTime,
		@RequestParam(value = "user", required = false) Long user) throws Exception{
        CustOrderBean custOrder0 = new CustOrderBean();
        if (!StringUtil.isBlank(name)) custOrder0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) custOrder0.setActive(activeBool);
        if (total != null) custOrder0.setTotal(total);
        if (amount != null) custOrder0.setAmount(amount);
        if (status != null) custOrder0.setStatus(status);
        if (paytype != null) custOrder0.setPaytype(paytype);
        if (!StringUtil.isBlank(phone)) custOrder0.setPhone(phone);
        if (!StringUtil.isBlank(address)) custOrder0.setAddress(address);
        if (orderDateTime != null ) {
            custOrder0.setOrderDateTime(orderDateTime);
        }
        if (user != null) custOrder0.setUser(user);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.pdf");
            List<CustOrderBean> list = service.searchCustOrdersByFields(custOrder0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user0 = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user0 != null) {
                    userType0 = userService.findUserById(user0);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterCustOrdersWord", method = RequestMethod.GET)
	public void filterCustOrdersWord(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "total", required = false) Double total,
		@RequestParam(value = "amount", required = false) Integer amount,
		@RequestParam(value = "status", required = false) Integer status,
		@RequestParam(value = "paytype", required = false) Integer paytype,
		@RequestParam(value = "phone", required = false) String phone,
		@RequestParam(value = "address", required = false) String address,
		@RequestParam(value = "orderDateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date orderDateTime,
		@RequestParam(value = "user", required = false) Long user) throws Exception{
        CustOrderBean custOrder0 = new CustOrderBean();
        if (!StringUtil.isBlank(name)) custOrder0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) custOrder0.setActive(activeBool);
        if (total != null) custOrder0.setTotal(total);
        if (amount != null) custOrder0.setAmount(amount);
        if (status != null) custOrder0.setStatus(status);
        if (paytype != null) custOrder0.setPaytype(paytype);
        if (!StringUtil.isBlank(phone)) custOrder0.setPhone(phone);
        if (!StringUtil.isBlank(address)) custOrder0.setAddress(address);
        if (orderDateTime != null ) {
            custOrder0.setOrderDateTime(orderDateTime);
        }
        if (user != null) custOrder0.setUser(user);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.doc");
            List<CustOrderBean> list = service.searchCustOrdersByFields(custOrder0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user0 = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user0 != null) {
                    userType0 = userService.findUserById(user0);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPPTCustOrders", method = RequestMethod.GET)
	public void filterPPTCustOrders(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "total", required = false) Double total,
		@RequestParam(value = "amount", required = false) Integer amount,
		@RequestParam(value = "status", required = false) Integer status,
		@RequestParam(value = "paytype", required = false) Integer paytype,
		@RequestParam(value = "phone", required = false) String phone,
		@RequestParam(value = "address", required = false) String address,
		@RequestParam(value = "orderDateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date orderDateTime,
		@RequestParam(value = "user", required = false) Long user) throws Exception{
        CustOrderBean custOrder0 = new CustOrderBean();
        if (!StringUtil.isBlank(name)) custOrder0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) custOrder0.setActive(activeBool);
        if (total != null) custOrder0.setTotal(total);
        if (amount != null) custOrder0.setAmount(amount);
        if (status != null) custOrder0.setStatus(status);
        if (paytype != null) custOrder0.setPaytype(paytype);
        if (!StringUtil.isBlank(phone)) custOrder0.setPhone(phone);
        if (!StringUtil.isBlank(address)) custOrder0.setAddress(address);
        if (orderDateTime != null ) {
            custOrder0.setOrderDateTime(orderDateTime);
        }
        if (user != null) custOrder0.setUser(user);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=CustOrders.pptx");
            List<CustOrderBean> list = service.searchCustOrdersByFields(custOrder0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "订单信息表";
            String [] headers = {"序号","名字","活跃","总价","商品总数","状态","支付方式","电话","地址","日期时间","用户"};
            Boolean [] isImages = {false,false,false,false,false,false,false,false,false,false,false};
            
            for (CustOrderBean custOrder:list) {
                Long user0 = custOrder.getUser();
                UserBean userType0;
                String userName0 = "";
                if (user0 != null) {
                    userType0 = userService.findUserById(user0);
                    if (userType0 !=null) userName0 = userType0.getUserName();
                }
                Object [] row = {StringUtil.toNullString(custOrder.getId()),custOrder.getName(),StringUtil.toNullString(custOrder.getActive()),StringUtil.toNullString(custOrder.getTotal()),StringUtil.toNullString(custOrder.getAmount()),StringUtil.toNullString(custOrder.getStatus()),StringUtil.toNullString(custOrder.getPaytype()),custOrder.getPhone(),custOrder.getAddress(),StringUtil.toNullString(custOrder.getOrderDateTime()),userName0};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/findCustOrderById", method = RequestMethod.POST)
	public Map<String,Object> findCustOrderById(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        CustOrderBean custOrder = service.findCustOrderById(id);
        result.put("success",true);
        result.put("data",custOrder);
        return result;
	}
	@RequestMapping(value = "/findCustOrderByName", method = RequestMethod.POST)
	public Map<String,Object> findCustOrderByName(@RequestParam(value = "name", required = true) String name) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        CustOrderBean custOrder = service.findCustOrderByName(name);
        result.put("success",true);
        result.put("data",custOrder);
        return result;
	}
	@RequestMapping(value = "/listActiveCustOrderOrderItemsUsingCustOrderId", method = RequestMethod.POST)
	public Map<String,Object> listActiveCustOrderOrderItemsUsingCustOrderId(@RequestParam (required = false) Long custOrderId) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        Set<OrderItemBean> set = new TreeSet<OrderItemBean>();
        if (custOrderId!=null) set = service.listActiveCustOrderOrderItemsUsingCustOrderId(custOrderId);
        result.put("success",true);
        result.put("rows",set);
        return result;
	}
	@RequestMapping(value = "/listActiveCustOrders", method = RequestMethod.POST)
	public Map<String,Object> listActiveCustOrders() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<CustOrderBean> list = service.listActiveCustOrders();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAllCustOrders", method = RequestMethod.POST)
	public Map<String,Object> listAllCustOrders() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<CustOrderBean> list = service.listAllCustOrders();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAvailableActiveCustOrderOrderItemsUsingCustOrderId", method = RequestMethod.POST)
	public Map<String,Object> listAvailableActiveCustOrderOrderItemsUsingCustOrderId(@RequestParam (required = false) Long custOrderId) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        Set<OrderItemBean> set = new TreeSet<OrderItemBean>();
        if (custOrderId!=null) set = service.listAvailableActiveCustOrderOrderItemsUsingCustOrderId(custOrderId);
        result.put("success",true);
        result.put("rows",set);
        return result;
	}
	@RequestMapping(value = "/revokeOrderItemsFromCustOrder", method = RequestMethod.POST)
	public Map<String,Object> revokeOrderItemsFromCustOrder(Long custOrderId,String orderItemIds) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        boolean success = service.revokeOrderItemsFromCustOrder(custOrderId,orderItemIds);
        result.put("success",success);
        result.put("rows",null);
        return result;
	}
	@RequestMapping(value = "/searchCustOrdersByFieldsByPage", method = RequestMethod.POST)
	public Map<String,Object> searchCustOrdersByFieldsByPage(@RequestParam(value = "page", required = false) Integer pagenum,
		@RequestParam(value = "rows", required = false) Integer pagesize,
		@RequestParam(value = "last", required = false) String lastFlag,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "total", required = false) Double total,
		@RequestParam(value = "amount", required = false) Integer amount,
		@RequestParam(value = "status", required = false) Integer status,
		@RequestParam(value = "paytype", required = false) Integer paytype,
		@RequestParam(value = "phone", required = false) String phone,
		@RequestParam(value = "address", required = false) String address,
		@RequestParam(value = "orderDateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date orderDateTime,
		@RequestParam(value = "user", required = false) Long user) throws Exception{
        CustOrderBean custOrder = new CustOrderBean();
        if (!StringUtil.isBlank(name)) custOrder.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) custOrder.setActive(activeBool);
        if (total != null) custOrder.setTotal(total);
        if (amount != null) custOrder.setAmount(amount);
        if (status != null) custOrder.setStatus(status);
        if (paytype != null) custOrder.setPaytype(paytype);
        if (!StringUtil.isBlank(phone)) custOrder.setPhone(phone);
        if (!StringUtil.isBlank(address)) custOrder.setAddress(address);
        if (orderDateTime != null) {
            custOrder.setOrderDateTime(orderDateTime);
        }
        if (user != null) custOrder.setUser(user);
        if (pagesize==null || pagesize <= 0) pagesize = 10;
        Integer recordCount = service.countSearchCustOrdersByFieldsRecords(custOrder);
        Integer pageCount = (int)Math.ceil((double)recordCount/pagesize);
        if (pageCount <= 1) pageCount = 1;
        if (pagenum==null || pagenum <= 1) pagenum = 1;
        if (pagenum >= pageCount) pagenum = pageCount;
        Boolean lastFlagBool = BooleanUtil.parseBoolean(lastFlag);
        if (lastFlagBool == null) lastFlagBool = false;
        if (lastFlagBool) pagenum = pageCount;
        Map<String,Object> result = new TreeMap<String,Object>();
        List<CustOrderBean> custOrderList = service.searchCustOrdersByFieldsByPage(custOrder,pagesize,pagenum);
        result.put("success",true);
        result.put("rows",custOrderList);
        result.put("total",recordCount);
        result.put("page",pagenum);
        return result;
	}
	@RequestMapping(value = "/softDeleteAllCustOrders", method = RequestMethod.POST)
	public Map<String,Object> softDeleteAllCustOrders(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteAllCustOrders(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/softDeleteCustOrder", method = RequestMethod.POST)
	public Map<String,Object> softDeleteCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleCustOrder", method = RequestMethod.POST)
	public Map<String,Object> toggleCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleOneCustOrder", method = RequestMethod.POST)
	public Map<String,Object> toggleOneCustOrder(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleOneCustOrder(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updateCustOrder", method = RequestMethod.POST)
	public Map<String,Object> updateCustOrder(@RequestBody CustOrderBean custOrder) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.updateCustOrder(custOrder);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
}

